package com.ABdolphin.forum.controller;

import com.ABdolphin.forum.common.AppResult;
import com.ABdolphin.forum.common.ResultCode;
import com.ABdolphin.forum.config.AppConfig;
import com.ABdolphin.forum.model.Article;
import com.ABdolphin.forum.model.User;
import com.ABdolphin.forum.services.IArticleService;
import com.ABdolphin.forum.services.IUserService;
import com.ABdolphin.forum.services.impl.ArticleServiceImpl;
import com.ABdolphin.forum.services.impl.UserServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/article")
@RestController
@Slf4j
@Tag(name = "Article")
public class ArticleController {
    @Resource(type = ArticleServiceImpl.class)
    private IArticleService articleService;

    @Resource(type = UserServiceImpl.class)
    private IUserService userService;

    @GetMapping("/getAllByBoardId")
    @Operation(summary = "根据版块id,获得文章列表")
    public AppResult<List<Article>> getAllByBoardId(@Parameter(name = "boardId", description = "版块id(可不填)") @RequestParam(value = "boardId", required = false) Long boardId) {
        List<Article> articles = null;
        if (boardId == null) {
            articles = articleService.selectAll();
        }else {
            articles = articleService.selectByBoardId(boardId);
        }
        if(articles==null){
            articles=new ArrayList<>();
        }
        return AppResult.success(articles);
    }

    @GetMapping("/getAllByUserId")
    @Operation(summary = "根据用户id,获得文章列表")
    public AppResult<List<Article>> getAllByUserId(HttpServletRequest request,
                                                   @Parameter(name = "userId", description = "用户id(可不填)") @RequestParam(value = "userId", required = false) Long userId) {
        if (userId==null){
            HttpSession session = request.getSession(false);
            User user = (User) session.getAttribute(AppConfig.USER_SESSION);
            userId=user.getId();
        }
        List<Article> articles = articleService.selectByUserId(userId);
        if (articles==null){
            articles=new ArrayList<>();
        }
        return AppResult.success(articles);
    }
    @PostMapping("/create")
    @Operation(summary = "新建一个文章")
    public AppResult create(HttpServletRequest request,
                            @Parameter(name = "boardId",description = "版块id")@RequestParam("boardId")@NotNull Long boardId,
                            @Parameter(name = "title",description = "文章标题")@RequestParam("title")@NotNull String title,
                            @Parameter(name = "content",description = "文章内容")@RequestParam("content")@NotNull String content){
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        Article article = new Article();
        article.setUserId(user.getId());
        article.setContent(content);
        article.setBoardId(boardId);
        article.setTitle(title);
        articleService.create(article);
        return AppResult.success();
    }
    @GetMapping("/details")
    @Operation(summary = "获得文章详情")
    public AppResult<Article> details(HttpServletRequest request,
                                      @Parameter(name = "id",description = "文章id")@RequestParam("id")@NotNull Long id){
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        Article article = articleService.selectDetailById(id);
        if (user.getId().equals(article.getUserId())){
            article.setOwn(true);
        }
        return AppResult.success(article);
    }
    @PostMapping("/modify")
    @Operation(summary = "修改文章")
    public AppResult modify(HttpServletRequest request,
                            @Parameter(name = "id",description = "文章id")@RequestParam("id")@NotNull Long id,
                            @Parameter(name = "title",description = "文章标题")@RequestParam("title")@NotNull String title,
                            @Parameter(name = "content",description = "文章内容")@RequestParam("content")@NotNull String content){
        if (id<=0){
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            return AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //作者时候否被禁言
        if (user.getState()==1){
            log.info(ResultCode.FAILED_USER_BANNED.toString());
            return AppResult.fail(ResultCode.FAILED_USER_BANNED);
        }
        //文章是否存在
        Article article = articleService.selectDetailById(id);
        if (article==null||article.getDeleteState()==1){
            log.info(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            return AppResult.fail(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //文章是否状态不正确
        if (article.getState()==1){
            log.info(ResultCode.FAILED_ARTICLE_BANNED.toString());
            return AppResult.fail(ResultCode.FAILED_ARTICLE_BANNED);
        }
        //文章是否是作者本人进行修改的
        if (!user.getId().equals( article.getUserId())){
            log.info(ResultCode.FAILED_ARTICLE_NOT_AUTHOR.toString());
            return AppResult.fail(ResultCode.FAILED_ARTICLE_NOT_AUTHOR);
        }
        //创建一个对象,进行修改
        Article paramArticle = new Article();
        paramArticle.setId(id);
        paramArticle.setTitle(title);
        paramArticle.setContent(content);
        articleService.modify(paramArticle);
        return AppResult.success();
    }
    @PostMapping("/thumbUp")
    @Operation(summary = "点赞")
    public AppResult thumbsUp (HttpServletRequest request,
                               @Parameter(name = "id",description = "文章id")@RequestParam("id")@NotNull Long id){
        User user = (User) request.getSession(false).getAttribute(AppConfig.USER_SESSION);
        if (user.getState()==1){
            log.info(ResultCode.FAILED_USER_BANNED.toString());
            return AppResult.fail(ResultCode.FAILED_USER_BANNED);
        }
        articleService.addOneArticleLikeCountById(id);
        return AppResult.success();
    }

    @PostMapping("/delete")
    @Operation(summary = "删除文章")
    public AppResult delete (HttpServletRequest request,
                             @Parameter(name = "id",description = "文章id")@RequestParam("id")@NotNull Long id){
        if (id<=0){
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            return AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        //文章是否存在
        Article article = articleService.selectDetailById(id);
        if (article==null||article.getDeleteState()==1){
            log.info(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            return AppResult.fail(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //是否是原作者
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if (!user.getId().equals(article.getUserId())){
            log.info(ResultCode.FAILED_ARTICLE_NOT_AUTHOR.toString());
            return AppResult.fail(ResultCode.FAILED_ARTICLE_NOT_AUTHOR);
        }
        //删除
        articleService.deleteById(id);
        //这里并不需要更新session,里面的值是多少无所谓
        return AppResult.success();
    }
}














